home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / comm / bbs / SentBy12.lha / SentBy.c < prev    next >
C/C++ Source or Header  |  1996-10-03  |  8KB  |  422 lines

  1.  
  2.  
  3.  
  4. // Besispiel an mainpartport text senden! (Derrow) -> SERVER_DirStringToNd
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13. #include <stdio.h>
  14. #include <strings.h>
  15. #include <proto/exec.h>
  16. #include <FAME/FAME.h>
  17.  
  18. char *VerStr={"$VER: SentBy 1.2 "__AMIGADATE__};
  19.  
  20. #define FILE_ID_STRUCT_SIZE 114 // Size of the structure
  21. #define FILE_ID_SENTBY_LINE 32  // Offset to the SentBy line
  22. #define FILE_ID_SENTBY_LEN  23  // SentBy line length without 0-byte
  23.  
  24. /* Extern the real ProgramName to use it instead of PROGDIR:Server
  25.  * _ProgramName contains the real name as the Server is named on start.
  26.  */
  27.  
  28. extern char    *_ProgramName;
  29.  
  30. /* Prototypes.
  31.  */
  32.  
  33. int AccessFile(char *InStr);
  34. void SPrintf(char *Buffer,char *ctl, ...);
  35. LONG GetFileSize(char *FileName);
  36. STRPTR MyTime(long longs, char InStr[]);
  37. void ShutDown(long);
  38.  
  39. /* Variables for ReadArgs and Template.
  40.  */
  41.  
  42. char     ArgStr[]="NODENR/K/N/A,USERSLOT/K/N/A,USERNAME/K/A,FILETYPE/K/A,FILEID/K/A,CONFPATH/K/A";
  43. char    FilePath[256],
  44.             FileIdPath[256],
  45.             UserName[256],
  46.             ConfLocation[102],
  47.             WorkStr[256];
  48. long    ArgArray[]={0L,0L,0L,0L,0L,0L};
  49. long    NodeNr,
  50.             UserSlot;
  51. BPTR    MyLock=NULL;
  52.  
  53. struct Library    *IconBase        = NULL;
  54. struct FAMEBase    *FAMEBase        = NULL;
  55.  
  56. void main(int argc, char *argv[])
  57. {
  58.     struct  RDArgs      *rda  = NULL;
  59.     struct    DiskObject    *dobj    =    NULL;
  60.  
  61.     Printf("\nSentBy v1.2 "__AMIGADATE__" by Strider (tRSi/F-iNNOVATiON)\n\nSentBy adds to fileids the for ByteAndi's FileId Door typical Sentby line.\nSentBy uses for this the Sent.dat file from ByteAndi/tRSi's FileId Door.\n");
  62.     Printf("SentBy is used for FAMEFileChecker background processing and must be\nstarted from FAMEFileChecker.\nJust copy SentBy and it's icon to: FAME:FileCheckers/FILEID/SentBy !\n\n");
  63.  
  64.     if( ! (IconBase = (struct Library *) OpenLibrary("icon.library",0L)))
  65.         ShutDown(4);
  66.  
  67.     if( ! (FAMEBase = (struct FAMEBase *) OpenLibrary(FAMENAME,FAMEVERSION)))
  68.         ShutDown(9);
  69.  
  70.     WorkStr[0] = '\0';
  71.  
  72.     /* Use ReadArgs() for arguments.
  73.      */
  74.  
  75.     if(rda = ReadArgs(ArgStr,ArgArray,NULL)) {
  76.  
  77.         /* Node number.
  78.          */
  79.  
  80.         if(ArgArray[0]) {
  81.  
  82.             long *LDummy = (long *)ArgArray[0];
  83.             NodeNr = (*LDummy);
  84.  
  85.         } else
  86.             ShutDown(1);
  87.  
  88.         /* User slot number.
  89.          */
  90.  
  91.         if(ArgArray[1]) {
  92.  
  93.             long *LDummy = (long *)ArgArray[1];
  94.             UserSlot = (*LDummy);
  95.  
  96.         } else
  97.             ShutDown(1);
  98.  
  99.         /* User name (not needed).
  100.          */
  101.  
  102.         if(ArgArray[2]) {
  103.  
  104.             STRPTR MyUNPtr = (STRPTR)ArgArray[2];
  105.  
  106.             if(strlen(MyUNPtr) > 30)
  107.                 MyUNPtr[30] = '\0';
  108.             strcpy(UserName,MyUNPtr);
  109.         } else
  110.             ShutDown(1);
  111.  
  112.         /* Path of the file id.
  113.          */
  114.  
  115.         if(ArgArray[4])
  116.             strcpy(FilePath,(STRPTR)ArgArray[4]);
  117.         else
  118.             ShutDown(1);
  119.  
  120.         /* Path of the Conference.
  121.          */
  122.  
  123.         if(ArgArray[5])
  124.             strcpy(ConfLocation,(STRPTR)ArgArray[5]);
  125.         else
  126.             ShutDown(1);
  127.  
  128.         FreeArgs(rda);
  129.     } else
  130.         ShutDown(1);
  131.  
  132.     /* Read the icon informations.
  133.      */
  134.  
  135.     if(dobj = GetDiskObject( _ProgramName )) {
  136.  
  137.         char        **oldtooltypes;
  138.         STRPTR    TempStr;
  139.  
  140.         oldtooltypes = dobj->do_ToolTypes;
  141.  
  142.         /* Location of ByteAndi's FILEID - door.
  143.          */
  144.  
  145.         if(TempStr = FindToolType(oldtooltypes,"FILEIDLOCATION")) {
  146.  
  147.             if(strlen(TempStr) > 255)
  148.                 TempStr[255] = '\0';
  149.             strcpy(FileIdPath,TempStr);
  150.         } else {
  151.  
  152.             FreeDiskObject(dobj);
  153.             ShutDown(3);
  154.         }
  155.  
  156.         FreeDiskObject(dobj);
  157.     } else
  158.         ShutDown(2);
  159.  
  160.     /* FileId Path ok ?
  161.      */
  162.  
  163.     if(AccessFile(FileIdPath))
  164.         ShutDown(5);
  165.  
  166.     /* Add the Sent.dat filename.
  167.      */
  168.  
  169.     if(AddPart(FileIdPath,"Sent.dat",255) == DOSFALSE)
  170.         ShutDown(6);
  171.  
  172.     /* FileId Path + Sent.dat ok ?
  173.      */
  174.  
  175.     if(AccessFile(FileIdPath))
  176.         ShutDown(7);
  177.  
  178.     /* Space for user in Sent.dat ?
  179.      */
  180.  
  181.     if(UserSlot > (GetFileSize(FileIdPath) / FILE_ID_STRUCT_SIZE))
  182.         WorkStr[0] = '\0';
  183.     else {
  184.  
  185.         /* Ok, let's get the SentBy.
  186.          */
  187.  
  188.         if(MyLock = Open(FileIdPath,MODE_OLDFILE)) {
  189.  
  190.             Seek(MyLock,(FILE_ID_STRUCT_SIZE * (UserSlot - 1)) + FILE_ID_SENTBY_LINE,OFFSET_BEGINNING);
  191.             Read(MyLock,WorkStr,FILE_ID_SENTBY_LEN + 1);
  192.             Close(MyLock);
  193.         } else
  194.             ShutDown(8);
  195.     }
  196.  
  197.     /* If WorkStr is empty we have to build our own SentBy line.
  198.      */
  199.  
  200.     if(WorkStr[0] == '\0') {
  201.  
  202.         STRPTR MyStrPtr;
  203.  
  204.         /* Cut user name length down to FILE_ID_SENTBY_LEN chars.
  205.          */
  206.  
  207.         if(strlen(UserName) > FILE_ID_SENTBY_LEN)
  208.             UserName[FILE_ID_SENTBY_LEN] = '\0';
  209.  
  210.         /* Fill up our buffer with FILE_ID_SENTBY_LEN spaces.
  211.          */
  212.  
  213.         FAMEStrFil(WorkStr,' ',FILE_ID_SENTBY_LEN);
  214.  
  215.         /* Take a pointer and set it to our now counted position.
  216.          */
  217.  
  218.         MyStrPtr = WorkStr + ((FILE_ID_SENTBY_LEN - strlen(UserName)) / 2);
  219.  
  220.         /* Copy the user name to this position.
  221.          */
  222.  
  223.         strcpy(MyStrPtr,UserName);
  224.  
  225.         /* Overwrite the 0-byte created by strcpy().
  226.          */
  227.  
  228.         if(strlen(WorkStr) < FILE_ID_SENTBY_LEN)
  229.             WorkStr[strlen(WorkStr)] = ' ';
  230.  
  231.         /* Just set on our FILE_ID_SENTBY_LEN position a new 0-byte.
  232.          */
  233.  
  234.         WorkStr[FILE_ID_SENTBY_LEN] = '\0';
  235.     }
  236.  
  237.     /* Ok, we now always have a centered or the users configured SentBy string.
  238.      * Let's add the rest and that's it.
  239.      */
  240.  
  241.     strcpy(UserName,WorkStr);
  242.     SPrintf(WorkStr,"[Node %2.2ld%23.23s [%s]\n",NodeNr,UserName,MyTime(time(NULL),FileIdPath));
  243.  
  244.     /* Ok, add it.
  245.      */
  246.  
  247.     if(MyLock = Open(FilePath,MODE_READWRITE)) {
  248.  
  249.         Seek(MyLock,0,OFFSET_END);
  250.         Write(MyLock,WorkStr,strlen(WorkStr));
  251.         Close(MyLock);
  252.     } else
  253.         ShutDown(10);
  254.  
  255.     Printf("Everything done !\n");
  256.  
  257.     ShutDown(0);
  258. }
  259.  
  260. int AccessFile(char InStr[])
  261. {
  262.     BPTR lock = NULL;
  263.  
  264.     if(lock = Lock(InStr,SHARED_LOCK)) {
  265.  
  266.         UnLock(lock);
  267.         return(0);
  268.     } else if(IoErr() == ERROR_OBJECT_IN_USE)
  269.         return(0);
  270.  
  271.     return(1);
  272. }
  273.  
  274. /* SPrintf via RawDoFmt:
  275.  *
  276.  * Note that you have to uses long aligned values means instead of only
  277.  * using %d you have to use %ld! Also %lc ...
  278.  */
  279.  
  280. void SPrintf(char *Buffer,char *ctl, ...)
  281.     {RawDoFmt(ctl, (long *)(&ctl + 1), (void (*))"\x16\xc0\x4e\x75",Buffer);}
  282.  
  283. LONG GetFileSize(char *FileName)
  284. {
  285.     struct FileInfoBlock    *FileInfo=NULL;
  286.     LONG             Size;
  287.  
  288.     if(FileInfo = (struct FileInfoBlock *)AllocDosObject(DOS_FIB,TAG_DONE)) {
  289.  
  290.         BPTR    FileLock;
  291.         if(FileLock = Lock(FileName,ACCESS_READ)) {
  292.  
  293.             if(Examine(FileLock,FileInfo)) {
  294.  
  295.                 if(FileInfo -> fib_DirEntryType < 0)
  296.                     Size = FileInfo -> fib_Size;
  297.             }
  298.             UnLock(FileLock);
  299.         }
  300.         FreeDosObject(DOS_FIB,FileInfo);
  301.     }
  302.  
  303.     return(Size);
  304. }
  305.  
  306. STRPTR MyTime(long longs, char InStr[])
  307. {
  308.   struct tm *ut = NULL;
  309.   char merk[31];
  310.  
  311.   ut = localtime(&longs);
  312.  
  313.   if(ut->tm_hour < 10)
  314.         SPrintf(InStr,"0%ld",ut->tm_hour);
  315.   else
  316.         SPrintf(InStr,"%ld",ut->tm_hour);
  317.  
  318.     strcat(InStr,":");
  319.  
  320.   if(ut->tm_min < 10)
  321.         SPrintf(merk,"0%ld",ut->tm_min);
  322.   else
  323.         SPrintf(merk,"%ld",ut->tm_min);
  324.  
  325.   strcat(InStr,merk);
  326.     strcat(InStr,":");
  327.  
  328.   if(ut->tm_sec < 10)
  329.         SPrintf(merk,"0%ld",ut->tm_sec);
  330.   else
  331.         SPrintf(merk,"%ld",ut->tm_sec);
  332.  
  333.   strcat(InStr,merk);
  334.  
  335.     return(InStr);
  336. }
  337.  
  338. void ShutDown(long error)
  339. {
  340.     long TheIoErr = 0L;
  341.  
  342.     if(FAMEBase)
  343.         CloseLibrary((struct Library *)FAMEBase);
  344.  
  345.     if(IconBase)
  346.         CloseLibrary((struct Library *)IconBase);
  347.  
  348.     switch(error)
  349.     {
  350.         case 1:
  351.             Printf("Argument error !\n");
  352.  
  353.             TheIoErr    = ERROR_REQUIRED_ARG_MISSING ;
  354.             error            = RETURN_ERROR;
  355.         break;
  356.         case 2:
  357.             Printf("Icon not found !\n");
  358.  
  359.             TheIoErr    = ERROR_OBJECT_NOT_FOUND ;
  360.             error            = RETURN_ERROR;
  361.         break;
  362.         case 3:
  363.             Printf("ToolType FILEIDLOCATION not found !\n");
  364.  
  365.             TheIoErr    = ERROR_OBJECT_NOT_FOUND ;
  366.             error            = RETURN_ERROR;
  367.         break;
  368.         case 4:
  369.             Printf("Can't open icon.library !\n");
  370.  
  371.             TheIoErr    = ERROR_INVALID_RESIDENT_LIBRARY ;
  372.             error            = RETURN_FAIL;
  373.         break;
  374.         case 5:
  375.             Printf("Can't find FileId path !\n");
  376.  
  377.             TheIoErr    = ERROR_DIR_NOT_FOUND ;
  378.             error            = RETURN_ERROR;
  379.         break;
  380.         case 6:
  381.             Printf("Error creating path !\n");
  382.  
  383.             TheIoErr    = 0L;
  384.             error            = RETURN_ERROR;
  385.         break;
  386.         case 7:
  387.             Printf("Can't find FileId Sent.dat file !\n");
  388.  
  389.             TheIoErr    = ERROR_OBJECT_NOT_FOUND ;
  390.             error            = RETURN_ERROR;
  391.         break;
  392.         case 8:
  393.             Printf("Can't open FileId Sent.dat file !\n");
  394.  
  395.             TheIoErr    = ERROR_OBJECT_WRONG_TYPE ;
  396.             error            = RETURN_ERROR;
  397.         break;
  398.         case 9:
  399.             Printf("Can't open FAME.library !\n");
  400.  
  401.             TheIoErr    = ERROR_INVALID_RESIDENT_LIBRARY ;
  402.             error            = RETURN_FAIL;
  403.         break;
  404.         case 10:
  405.             Printf("Can't add SentBy line to file !\n");
  406.  
  407.             TheIoErr    = ERROR_OBJECT_WRONG_TYPE ;
  408.             error            = RETURN_ERROR;
  409.         break;
  410.         default:
  411.             TheIoErr    = 0L;
  412.             error            = RETURN_OK;
  413.         break;
  414.     }
  415.  
  416.     Printf("\n");
  417.  
  418.     SetIoErr( TheIoErr );
  419.  
  420.     exit(error);
  421. }
  422.